package com.star.mall.persistence.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.star.mall.base.exception.BusinessException;
import com.star.mall.file.FileService;
import com.star.mall.persistence.entity.FileRecord;
import com.star.mall.persistence.mapper.FileRecordMapper;
import com.star.mall.persistence.service.IFileRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.star.mall.utils.UniqueIdUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.Stream;

/**
 * <p>
 * 文件表  服务实现类
 * </p>
 *
 * @author StarQ
 * @since 2021-05-09
 */
@Service
@Log4j2
public class FileRecordServiceImpl extends ServiceImpl<FileRecordMapper, FileRecord> implements IFileRecordService {

    @Value("${system.file.mode}")
    private String mode;
    @Resource
    FileService fileService;

    @Override
    @Transactional
    public FileRecord uploadFile(MultipartFile file) {
        try(InputStream inputStream = file.getInputStream()) {
            FileRecord fileRecord = new FileRecord();
            fileRecord.setId(UniqueIdUtil.getSid());
            fileRecord.setName(FileNameUtil.getPrefix(file.getOriginalFilename()));
            fileRecord.setExt(FileNameUtil.extName(file.getOriginalFilename()));
            fileRecord.setType(mode);
            fileService.uploadFile(fileRecord, inputStream);
            save(fileRecord);
            return fileRecord;
        }catch (IOException e) {
            log.error(e.getMessage());
            throw new BusinessException("上传文件读取失败");
        }
    }

    @Override
    public void downloadFile(String id, HttpServletResponse response) {
        FileRecord fileRecord = getById(id);
        if (ObjectUtil.isEmpty(fileRecord)) {
            throw new BusinessException("文件不存在");
        }
        response.setHeader("Content-Disposition", "attachment; filename=" + fileRecord.getName());
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            fileService.downloadFile(fileRecord.getPath(), fileRecord.getKey() + "." + fileRecord.getExt(), outputStream);
        }catch (IOException e) {
            log.error("下载失败，原因：{}",e.getMessage());
        }
    }

    @Override
    @Transactional
    public void deleteFile(String... ids) {
        Stream.of(ids).forEach(id -> {
            FileRecord fileRecord = getById(id);
            removeById(id);
            fileService.deleteFile(fileRecord.getPath(), fileRecord.getKey() + "." + fileRecord.getExt());
        });
    }
}
